package com.dianrui.qiyouriding.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.event.RfreshWallet;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.dianrui.qiyouriding.view.jwcounttime.SecondDownTimerViewWithDay;
import com.dianrui.qiyouriding.view.jwcounttime.base.OnCountDownTimerListener;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 退款中/退款成功
 */
@SuppressWarnings("ALL")
public class RefundDespotiSuccessActivity extends BaseActivity {
    private String settingId;

    @BindView(R.id.txt_success)
    TextView txtSuccess;
    @BindView(R.id.count_time_layout)
    LinearLayout countTimeLayout;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.show_moneys)
    TextView showMoneys;
    @BindView(R.id.count_time)
    SecondDownTimerViewWithDay countTime;


    @Override
    public int getContentViewId() {
        return R.layout.activity_refund_success;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                String refundMoney = bundle.getString("money");
                settingId = bundle.getString("settingid");
                showMoneys.setText(refundMoney);
            }
        }
        title.setText(getString(R.string.withdraw_title));
        customInit(true, R.color.main_color);
        getWithDraws();

    }

    @Override
    public void initData() {}


    private void getWithDraws() {
        JsonObject withdraw = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            withdraw.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        withdraw.addProperty("setting_id", settingId);
        OkHttpRequest.getInstance().postRequests(Url.WITH_DRAWS, withdraw.toString(), new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                                showMoneys.setText(jsonObject.optJSONObject("data").optString("deposit"));
                                int times = jsonObject.optJSONObject("data").optInt("time");
                                if (times > 0) {
                                    txtSuccess.setText(getString(R.string.withdraw_title));
                                    countTime.setDownTime((long) (jsonObject.optJSONObject("data").optInt("time") * 1000));
                                    countTime.setDownTimerListener(new OnCountDownTimerListener() {
                                        @Override
                                        public void onTick(long millisUntilFinished) {
                                        }

                                        @Override
                                        public void onFinish() {
                                            txtSuccess.setText(getString(R.string.withdraw_success));
                                            title.setText(getString(R.string.withdraw_success));

                                        }
                                    });
                                    countTimeLayout.setVisibility(View.VISIBLE);
                                    countTime.startDownTimer();
                                } else if (times <= 0) {
                                    countTime.setVisibility(View.GONE);
                                    countTimeLayout.setVisibility(View.GONE);
                                }
                            } else {
                                countTimeLayout.setVisibility(View.GONE);
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                countTimeLayout.setVisibility(View.GONE);
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @OnClick({R.id.back, R.id.refund_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                if (countTime != null) {
                    countTime.cancelDownTimer();
                }
                JumpActivitys(MyActivity.class);
                EventBus.getDefault().post(new RfreshWallet());
                finish();
                break;

            case R.id.refund_btn:
                if (countTime != null) {
                    countTime.cancelDownTimer();
                }
                JumpActivitys(MyActivity.class);
                EventBus.getDefault().post(new RfreshWallet());
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (countTime != null) {
                countTime.cancelDownTimer();
            }
            EventBus.getDefault().post(new RfreshWallet());
            JumpActivitys(MyActivity.class);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (countTime != null) {
            countTime.cancelDownTimer();
        }
    }


}
